//
//  GQHCustomerServicesController.m
//  Xanthium
//
//  Created by GuanQinghao on 07/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHCustomerServicesView.h"

#pragma mark Controller
#import "GQHCustomerServicesController.h"

#pragma mark CocoaPods
#import <UIImageView+WebCache.h>

#pragma mark ----------Header End----------

@interface GQHCustomerServicesController () <GQHCustomerServicesViewDelegate> {
    
#pragma mark Data
    
    NSMutableArray *contentArray;
    NSString *wechatQRPath;
    NSString *QQQRPath;
#pragma mark View
    /// 自定义根视图
    GQHCustomerServicesView *rootView;
    UIView *containerView;
}

@end


@implementation GQHCustomerServicesController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHCustomerServicesView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    contentArray = [NSMutableArray array];
    [self fetchCompanyDetails];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"智运客服";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    containerView.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeContainerView:)];
    [containerView addGestureRecognizer:tapGesture];
    
    UIImageView *QRImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    QRImageView.qh_centerX = rootView.qh_centerX;
    QRImageView.qh_centerY = rootView.qh_centerY;
    [containerView addSubview:QRImageView];
    
    switch (indexPath.row) {
        case 0: {
            
            if (contentArray[0]) {
                
                [self qh_dialWithMobile:contentArray[0]];
            }
        }
            break;
        case 1: {
            
            [QRImageView sd_setImageWithURL:[NSURL URLWithString:wechatQRPath] placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
            
            [rootView addSubview:containerView];
            [rootView bringSubviewToFront:containerView];
        }
            break;
        case 2: {
            
            
            
            [QRImageView sd_setImageWithURL:[NSURL URLWithString:QQQRPath] placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
            [rootView addSubview:containerView];
            [rootView bringSubviewToFront:containerView];
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchCompanyDetails {
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/config/1/getCompanyInfo"];
    [GQHNetHelper qh_requestGET:URLString parameters:nil success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@-%@",message,data);
        
        if (0 == [code intValue]) {
            
            [contentArray qh_safetySetObject:[data objectForKey:@"a4127"] atIndex:0];
            [contentArray qh_safetySetObject:[data objectForKey:@"a4118"] atIndex:1];
            [contentArray qh_safetySetObject:[data objectForKey:@"a4126"] atIndex:2];
            
            NSString * url = @"http://wccy.jt169.com";
            wechatQRPath = [NSString stringWithFormat:@"%@%@",url,[data objectForKey:@"a4128"]];
            QQQRPath = [NSString stringWithFormat:@"%@%@", url,[data objectForKey:@"a4129"]];
            
            rootView.qh_codeArray = contentArray;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
    }];
    
}

- (IBAction)removeContainerView:(id)sender {
    
    [containerView removeFromSuperview];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
